package com.chenlei.toppopup.adapter;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chenlei.toppopup.CLTopPopup;
import com.chenlei.toppopup.R;
import com.chenlei.toppopup.model.CLModel;
import com.chenlei.toppopup.util.DrawableUtils;
import com.chenlei.toppopup.util.UnitUtils;
import com.chenlei.toppopup.viewholder.ViewHolder;

import java.util.List;

/**
 * Created by chenlei on 2017/12/13.
 */

public class CLTopPopupAdapter extends CLBasePopupAdapter<CLModel>
{
  private List<CLModel> mDatas;//数据
  private int mIndex;//选中

  private EditText mBeforeTV;//上个选中文本
  private LinearLayout mBeforeLayout;

  //文本样式
  private CLTopPopup.TextBuilder textBuilder;
  private CLTopPopup.LeftEndTextBuilder leftEndTextBuilder;

  public CLTopPopupAdapter(Context context, CLTopPopup.TextBuilder builder, List<CLModel> datas, int selectIndex)
  {
    this(context,builder,null,datas,selectIndex);
  }

  public CLTopPopupAdapter(Context context, CLTopPopup.TextBuilder builder, CLTopPopup.LeftEndTextBuilder endTextBuilder, List<CLModel> datas, int selectIndex)
  {
    super(context, datas, R.layout.cl_toppopup_adapter_item);
    leftEndTextBuilder = endTextBuilder;
    textBuilder = builder;
    mIndex = selectIndex;
    mDatas = datas;
  }

  @Override
  public void convert(final ViewHolder holder, CLModel clModel, final int position)
  {
    final EditText tv = holder.getView(R.id.cl_toppopup_adapter_textview);
    final View line = holder.getView(R.id.cl_toppopup_adapter_line);
    final LinearLayout layout = holder.getView(R.id.cl_toppopup_adapter_bg);

    //左侧最后一个自定义
    int height;
    int textSize;

    if (leftEndTextBuilder != null && mDatas.size() != 0 && position == mDatas.size()-1)
    {
      //隐藏间隔线
      line.setVisibility(View.GONE);

      height = leftEndTextBuilder.getHeight();
      textSize = leftEndTextBuilder.getTextSize();

      int textColor = Color.BLACK;
      if (leftEndTextBuilder.getTextColor() != 0)
        textColor = ContextCompat.getColor(mContext,leftEndTextBuilder.getTextColor());
      tv.setTextColor(textColor);
    }
    else
    {
      //间隔线设置
      if (!textBuilder.isShowLine())
      {
        line.setVisibility(View.GONE);
      }
      else
      {
        line.setVisibility(View.VISIBLE);
        int lineColor = Color.parseColor("#f2f2f2");
        if (textBuilder.getLineColor() != 0)
          lineColor = ContextCompat.getColor(mContext,textBuilder.getLineColor());
        line.setBackgroundColor(lineColor);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) line.getLayoutParams();
        layoutParams.setMarginStart(UnitUtils.dip2px(mContext,textBuilder.getLineMarginStart()));
        layoutParams.setMarginEnd(UnitUtils.dip2px(mContext,textBuilder.getLineMarginEnd()));
        line.setLayoutParams(layoutParams);
      }

      //文本左侧对齐
      if (textBuilder.isTextLeft() || clModel.isLeft()){
        tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
      }else{
        tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
      }

      tv.setPadding(UnitUtils.dip2px(mContext,textBuilder.getTextMarginStart()), 0, UnitUtils.dip2px(mContext,textBuilder.getTextMarginEnd()), 0);

      height = textBuilder.getHeight();
      textSize = textBuilder.getTextSize();

      //设置文本背景选择
      if (textBuilder.getNormalBgColor() != 0 && textBuilder.getSelectBgColor() != 0){
        ColorDrawable nd = new ColorDrawable(ContextCompat.getColor(mContext,textBuilder.getNormalBgColor()));
        ColorDrawable sd = new ColorDrawable(ContextCompat.getColor(mContext,textBuilder.getSelectBgColor()));
        if (!clModel.isSelected())
          sd = nd;
        StateListDrawable stateListDrawable = DrawableUtils.getSelectorDrawable(nd,sd);
        layout.setBackground(stateListDrawable);
      }

      //设置文本选择图标  右侧
      if (textBuilder.getNormalDrawable() != 0 && textBuilder.getSelectDrawable() != 0){
        Drawable nd;
        Drawable sd;
        if (clModel.getRightDrawable() != 0){
          nd = ContextCompat.getDrawable(mContext,clModel.getRightDrawable());
          sd = ContextCompat.getDrawable(mContext,clModel.getRightSelectDrawable());
        }else{
          nd = ContextCompat.getDrawable(mContext,textBuilder.getNormalDrawable());
          sd = ContextCompat.getDrawable(mContext,textBuilder.getSelectDrawable());
        }

        if (!clModel.isSelected())
          sd = nd;
        StateListDrawable stateListDrawable = DrawableUtils.getSelectorDrawable(nd,sd);
        stateListDrawable.setBounds(0, 0, UnitUtils.dip2px(mContext,height/2), UnitUtils.dip2px(mContext,height/2));
        tv.setCompoundDrawables(null,null,stateListDrawable,null);
      }

      //设置文本选择颜色
      int nColor = Color.GRAY;
      int sColor = Color.BLACK;
      if (textBuilder.getSelectTextColor() != 0 && textBuilder.getNormalTextColor() != 0){
        nColor = ContextCompat.getColor(mContext,textBuilder.getNormalTextColor());
        sColor = ContextCompat.getColor(mContext,textBuilder.getSelectTextColor());
      }
      if (!clModel.isSelected())
        sColor = nColor;
      ColorStateList colorStateList = DrawableUtils.getSelectorColor(nColor,sColor);
      tv.setTextColor(colorStateList);
      tv.setHintTextColor(colorStateList);
    }

    int maxLength = 50;
    //设置文本信息
    tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,UnitUtils.sp2px(mContext,textSize));
    if (clModel.isEditText())
    {
      tv.setHint(clModel.getText());
      tv.setFocusable(true);
      tv.setFocusableInTouchMode(true);
      tv.setText("");
      maxLength = clModel.getMaxLength();
    }else{
      tv.setText(clModel.getText());
      tv.setHint("");
      tv.setFocusable(false);
      tv.setFocusableInTouchMode(false);
    }
    tv.requestFocus();

    tv.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});

    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) tv.getLayoutParams();
    layoutParams.height = UnitUtils.dip2px(mContext,height);
    tv.setLayoutParams(layoutParams);

    //设置是否选择
    if (position == mIndex)
    {
      tv.setSelected(true);
      layout.setSelected(true);
      mBeforeTV = tv;
      mBeforeLayout = layout;
    }
    else
    {
      tv.setSelected(false);
      layout.setSelected(false);
    }

    tv.addTextChangedListener(new TextWatcher() {
      @Override
      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

      }

      @Override
      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        mListener.onItemInputText(charSequence.toString());
      }

      @Override
      public void afterTextChanged(Editable editable) {

      }
    });

    tv.setOnClickListener(new View.OnClickListener()
    {
      @Override
      public void onClick(View v)
      {
        if (mBeforeTV != null)
        {
          mBeforeTV.setSelected(false);
          mBeforeTV.setCursorVisible(false);
        }
        if (mBeforeLayout != null)
          mBeforeLayout.setSelected(false);
        if (!tv.isFocusable())
          tv.setCursorVisible(false);
        else
          tv.setCursorVisible(true);
        tv.setSelected(true);
        layout.setSelected(true);
        mListener.onItemClick(position);
        mIndex = position;
        mBeforeTV = tv;
        mBeforeLayout = layout;
      }
    });

  }

  public void setSelectIndex(int selectIndex){
    mIndex = selectIndex;
  }
}
